package kburndown.client.app.controls
{
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	
	import mx.core.UIComponent;

	public class UnitItemShadow extends UIComponent
	{
		// --------------------------------------------------
		//
		//		类定义
		//
		// --------------------------------------------------
		
		// --------------------------------------------------
		//		常量
		// --------------------------------------------------
		private const SHADOW_WIDTH: int = 40;
		
		
		// --------------------------------------------------
		//		公共成员
		// --------------------------------------------------
		public function showShadow():void
		{
			shadowSprite.visible = true;
			startAnimation();
		}
		
		public function hideShadow():void
		{
			shadowSprite.visible = false;
			stopAnimation();
		}

		
		// --------------------------------------------------
		//		构造函数
		// --------------------------------------------------
		public function UnitItemShadow()
		{
			super();

			shadowSprite = new Sprite();
			shadowSprite.x = -SHADOW_WIDTH;
			shadowSprite.visible = false;
			this.addChild(shadowSprite);
			g = shadowSprite.graphics;

			maskSprite = new Sprite();
			this.addChild(maskSprite);
			this.mask = maskSprite;
		}

		
		// --------------------------------------------------
		//		私有成员
		// --------------------------------------------------
		private var shadowSprite: Sprite;
		private var g: Graphics;
		private var maskSprite: Sprite;

		private function drawShadows(): void
		{
			g.clear();
			var mtx:Matrix = new Matrix();
			mtx.createGradientBox(this.width, this.height, 90);
			g.beginGradientFill(GradientType.LINEAR, [0xFFFFFF, 0xFFFFFF], [0.25, .35], [0x00, 0xFF], mtx); 

			var pathCommands: Vector.<int> = new Vector.<int>(4, true);
			var pathCoords: Vector.<Number> = new Vector.<Number>(8, true);

			pathCommands[0] = 1;
			pathCommands[1] = 2;
			pathCommands[2] = 2;
			pathCommands[3] = 2;

			var stripCount: int = this.width / SHADOW_WIDTH + 2;

			for (var i: int = 0; i < stripCount; i++)
			{
				var offsetX: int = SHADOW_WIDTH * i;

				pathCoords[0] = SHADOW_WIDTH / 2 + offsetX;
				pathCoords[1] = 0;
				pathCoords[2] = 0 + offsetX;
				pathCoords[3] = this.height;
				pathCoords[4] = SHADOW_WIDTH / 2 + offsetX;
				pathCoords[5] = this.height;
				pathCoords[6] = SHADOW_WIDTH + offsetX;
				pathCoords[7] = 0;

				g.drawPath(pathCommands, pathCoords);
			}
		}

		private function setupMask(): void
		{
			maskSprite.graphics.beginFill(0xFF9900);
			maskSprite.graphics.drawRoundRect(0, 0, this.width, this.height, 15, 15);
		}
		
		private var _moving:Boolean = false;
		
		private function startAnimation():void
		{
			if (!_moving)
			{
				this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
				_moving = true;
			}
		}
		
		private function stopAnimation():void
		{
			if (_moving)
			{
				this.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
				shadowSprite.x = -SHADOW_WIDTH;
				_moving = false;
			}
		}

		
		// --------------------------------------------------
		//
		//		父类/接口方法实现
		//
		// --------------------------------------------------
		
		// --------------------------------------------------
		//		UIComponent : updateDisplayList
		// --------------------------------------------------
		override protected function updateDisplayList(unscaledWidth: Number, unscaledHeight: Number): void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);

			drawShadows();
			setupMask();
		}
		
		
		// --------------------------------------------------
		//
		//		事件侦听
		//
		// --------------------------------------------------
		protected function enterFrameHandler(event:Event):void
		{
			shadowSprite.x += 1;
			if (shadowSprite.x >= 0) shadowSprite.x = -SHADOW_WIDTH;
		}
	}
}